﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dralee.IdentityServer.Host.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Dralee.IdentityServer.Host
{
    public class Startup
    {
        private IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            // 通过数据库
            //var connectionString = Configuration.GetConnectionString("Default");
            //services.AddDbContext<DraleeAdminContext>(options => options.UseSqlServer(connectionString));
            //services.AddIdentity<AbpUsers, AbpRoles>()
            //    .AddEntityFrameworkStores<DraleeAdminContext>()
            //    .AddDefaultTokenProviders();
            services.AddOptions();
            // 通过内存
            var config = new IdentityServerCfg();
            Configuration.GetSection("IdentityServer").Bind(config);
            services.AddIdentityServer()
                .AddDeveloperSigningCredential()
                .AddInMemoryPersistedGrants()
                .AddInMemoryIdentityResources(IdentityServerConfig.GetIdentityResources())
                .AddInMemoryApiResources(IdentityServerConfig.GetApiResources(config))
                //.AddInMemoryClients(IdentityServerConfig.GetClients(Configuration)) // 2.2此种方式获取不到内容
                .AddInMemoryClients(IdentityServerConfig.GetClients(config))
            //.AddAspNetIdentity<AbpUsers>();
            // 从数据库读取配置等内存(clients, resources)
            //.AddConfigurationStore(options =>
            //{
            //    options.ConfigureDbContext = b => b.UseSqlServer(connectionString);
            //})
            //// this adds the operational data from DB(codes,tokens,consents)
            //.AddOperationalStore(options =>
            //{
            //    options.ConfigureDbContext = b => b.UseSqlServer(connectionString);
            //    options.PersistedGrants.Name = "AbpPersistedGrants";
            //    //options.DeviceFlowCodes.Name = "";
            //    // this enables automatic token cleanup. this is optional.
            //    options.EnableTokenCleanup = true;
            //});

            //.AddAspNetIdentity()
            //.AddAbpPersistedGrants<AdminDbContext>()
            //.AddAbpIdentityServer<User>();
            ;

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseIdentityServer();
        }
    }
}
